# import sys
# sys.path.append(r"E:\HelloWorld\Python\code\api_auto_test\cases\pack01_unittest")
# import user_login
# print(user_login.login(1,2))

# import os
# import sys
# sys.path.append(os.path.join(os.path.dirname(__file__), '..\mockapi'))
# import user_login
# print(user_login.login(1,2))
# print(sys.path)

# import requests

from cacheout import Cache, LFUCache
import time
import os
import sys
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# from api.login_demo_api1 import login
from utils.basic_utils import set_cache, get_cache
# print(sys.path)

if __name__ == '__main__':
    cache1 = Cache()
    cache1.set('tcache', 'cache1234567')
    cache1.set_many({'a': 1, 'b': 2, 'c': 3})  #批量设置
    print(cache1.items())  #全部键值对
    print(cache1.keys())
    print(cache1.values())
    print("cache1.get('tcache') : {}", cache1.get('tcache'))  #按Key取值
    print("ache1.get_many(['a', 'c']): {}", cache1.get_many(['a', 'c']))  #批量get
    print(len(cache1))
    # cache1.delete('a')
    # cache1.delete_many(['a', 'c'])

    cache2 = LFUCache()
    cache2.set('LFUk', 'LFUv12321')
    print(cache2.get('LFUk'))

    # 为每个键值对设置存活过期时间,并验证
    cache2.set(3, {'data': {}}, ttl=1)
    print(cache2.get(3) == {'data': {}})
    time.sleep(0.5)
    print(cache2.get(3) is not None)
    time.sleep(0.5)
    print(cache2.get(3) is not None)

    from cachetools import cached  #使用时间模块来查看模块的效率

    # without cached 未使用缓存
    def fib(n):
        return n if n < 2 else fib(n - 1) + fib(n - 2)

    # Use this decorator to enable caching 使用缓存
    # @cached(cache={})
    @cache1.memoize()
    def fib1(n):
        return n if n < 2 else fib1(n - 1) + fib1(n - 2)

    s = time.time()
    print(fib(25))
    print("without cached Time Taken:", time.time() - s)

    # Now using cached
    s1 = time.time()
    print(fib1(25))
    print("cached Time Taken(cached): ", time.time() - s1)
    # set_cache('key1', '测试添加')

    cache3 = Cache()

    @cache3.memoize()  #函数使用缓存
    def func1(a, b):
        return a + b

    func1(1, 2)
    print("cache3:{}", cache3.items())
    cache3.delete(list(cache3.keys())[0])
    print("cache3 del key:{}", cache3.items())
    # cache3.delete
    print("cache1:{}", cache1.items())
    cache1.clear()
    print("cache1 clear:{}", cache1.items())
